//
//  OriginalStatusView.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//个人详细数据内容
class OriginalStatusView: UIView {

    // 当前控件的底部约束
    var bottonCons: Constraint?
    
    // 要拿到数据，才能让当前视图身上的控件去显示数据

    var statuViewModel : Homecellviewmdoel?{
        
        didSet{
            
            // 设置头像
            headImage.sd_setImageWithURL(NSURL(string: statuViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            // 设置昵称
            nameLabel.text = statuViewModel?.status?.user?.name            // 设置会员图标
            memberImageView.image = statuViewModel?.memberImage
                // TODO : 时间与来源 (会重新设置)
            timeLabel.text = statuViewModel?.status?.created_at
            sourceLabel.text = statuViewModel?.sourceText
            
            // 设置微博正文内容
            contentLabel.text = statuViewModel?.status?.text
            
            // 0. 卸载之前的底部约束
            bottonCons?.uninstall()
            
//            设置配图的数据
            
            //     判断如果是1张图片，设置itemSize为配图视图的大小
            if let pic_urls = statuViewModel?.status?.pic_urls where pic_urls.count > 0 {
                
//                // 判断如果是1张图片，设置itemSize为配图视图的大小
//                if pic_urls.count == 1 {
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statuViewModel!.originalPictureViewSize
//                }else{
//                    // 否则设置成正方形 HMStatusPictureItemWH
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
//                }
//                
//                
//                // 1. 设置数据 (根据数据计算出配图视图的大小并设置上去)
////                pictureView.pic_urls = pic_urls
//
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                make.size.equalTo(statuViewModel?.originalPictureViewSize ?? CGSizeZero)
//
//                })
//                statuViewModel?.originalPictureViewSize
//                
                pictureView.dataInfo = (pic_urls, statuViewModel!.originalPictureViewSize)
                

                // 2. 显示控件
                pictureView.hidden = false
                
                // 3. 更新约束  原创微博视图的底部等于配图视图的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                })
                
            }else{
                // 1. 隐藏控件
                pictureView.hidden = true
                
                // 2. 更新约束 原创微博视图的底部等于文字label的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonCons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
            }
        }
    }
    
 
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private func setupUI(){
            backgroundColor = UIColor.whiteColor()
            
            // 添加控件
            addSubview(headImage)
            addSubview(nameLabel)
            addSubview(memberImageView)
            addSubview(timeLabel)
            addSubview(sourceLabel)
            addSubview(verifiedImageView)
            addSubview(contentLabel)
            addSubview(pictureView)
            
            // 添加约束
            // 头像
            headImage.snp_makeConstraints { (make) -> Void in
                make.top.leading.equalTo(HMStatusCellMargin)
                make.size.equalTo(CGSize(width: HMStatusCellHeadImgWH, height: HMStatusCellHeadImgWH))
            }
            
            // 约束昵称
            nameLabel.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(headImage)
                make.leading.equalTo(headImage.snp_trailing).offset(HMStatusCellMargin)
            }
            // 会员
            memberImageView.snp_makeConstraints { (make) -> Void in
                make.centerY.equalTo(nameLabel)
                make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            }
            
            // 时间
            timeLabel.snp_makeConstraints { (make) -> Void in
                make.bottom.equalTo(headImage)
                make.leading.equalTo(nameLabel)
            }
            
            // 来源
            sourceLabel.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
                make.top.equalTo(timeLabel)
            }
            // 认证
            verifiedImageView.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(headImage.snp_trailing).offset(-2)
                make.centerY.equalTo(headImage.snp_bottom).offset(-2)
            }
            //r 微博正文内容显示的label
            contentLabel.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(headImage)
                make.top.equalTo(headImage.snp_bottom).offset(HMStatusCellMargin)
            }
            
            pictureView.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(contentLabel)
                make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
                // 以下代码仅供测试
                make.size.equalTo(CGSize(width: 100, height: 100))
            }
            
            
            self.snp_makeConstraints { (make) -> Void in
                bottonCons = make.bottom.equalTo(pictureView.snp_bottom).offset(HMStatusCellMargin).constraint
            }
        }
        
        
    // MARK: - 懒加载控件
    
    // 头像
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    
    // 昵称的label
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellNameFontSize)
        label.text = "哈哈"
        return label
    }()
    
    // 会员图片
    private lazy var memberImageView: UIImageView = UIImageView(image: UIImage(named:"common_icon_membership"))
    
    // 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: HMStatusCellTimeFontSize)
        label.text = "刚刚"
        return label
    }()
    
    // 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellTimeFontSize)
        label.text = "来自 黑马微博"
        return label
    }()
    
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    // 微博正文内容显示的label
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
        return label
    }()
    
    
    /// 配图视图
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())


}
